<template>
	<view class="container">
		<uni-uform v-model="form" :label-width="85" ref="uform" :config="config" :rules="rules"
			validate-trigger="bind" :label-align="align" :show-border="true">
			<template #position>
				<uni-easyinput suffixIcon="location-filled" v-model="form.address" placeholder="点击选择具体位置" disabled @iconClick="posClick"></uni-easyinput>
			</template>
			<template #disposeUser>
				<uni-easyinput suffixIcon="staff" v-model="form.disposeUserName" placeholder="点击选择交办对象" @iconClick="userClick" disabled></uni-easyinput>
			</template>
			<template #attach>
				<!-- <uni-file-picker v-model="files" ref="fileUpload" limit="9" mode="grid"  title="最多选择9张图片" @select="fileUploadSelect" :auto-upload="false" @delete="deleteImg" :readonly="isView"></uni-file-picker> -->
				<common-attach :files="files" ref="attach" :limit="9" :readonly="isView"></common-attach>
			</template>
		</uni-uform>
		<view class="footer-btn-content" style="margin-top: 10px;">
			<button v-if="!isView" size="mini" class="common-mini-btn primary-btn" @click="handleSubmit">提交</button>
			<button v-if="isDeal" size="mini" class="common-mini-btn primary-btn" @click="handleDeal">提交</button>
			<button size="mini" class="common-mini-btn other-btn" @click="handleCancel">关闭</button>
		</view>
		<uni-popup ref="popup" type="bottom" background-color="#fff">
			<view class="popup-content">
				<user-select @change="radioChange" :orgid="orgId"></user-select>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { getUUID } from '@/utils/common'
	import {  getCaseAssignedList,addCawCaseAssigned,getCaseAssignedDetail,updateCawCaseAssigned} from '@/api/dums/caw/caseAssigned.js'
	import  config from '@/config'
	import { getAttachList } from "@/api/dums/am/attach";
	export default {
		data() {
			return {
				align:"left",
				form: { // 建议申明属性与config数组的name保持一致
					assignedCaseName: "",
					assignedDescription: "",
					lnglat:"",
					address:"",
					disposeUserName:"",
					disposeUserId:""
				},
				config: [{
						name: 'assignedCaseName',
						label: '事项简称',
						component: 'uni-easyinput',
						extra: {
							placeholder: "事项简称",
							required:true
						}
					},{
						name: 'position',
						label: '具体位置',
						isSlot:true
					},{
						name: 'assignedDescription',
						label: '情况描述',
						component: 'uni-easyinput',
						extra: {
							type: 'textarea',
							placeholder: "情况描述",
							required:true
						}
					}, {
						name: 'disposeUser',
						label:'交办对象',
						isSlot:true
					},{
						name: 'attach',
						label:'附件',
						isSlot:true
					}
				],
				rules: {
					assignedCaseName: {
						rules: [{
							required: true,
							errorMessage: '不能为空',
						},{
							maxLength:25,
							errorMessage: '内容超长'
						}]
					},
					assignedDescription: {
						rules: [{
								required: true,
								errorMessage: '不能为空',
							}
						]
					},
					disposeDescription:{
						rules: [{
								errorMessage: '不能为空',
							}
						]
					}
				},
				files:[],
				// filePaths:[],
				orgId: this.$store.state.user.orgid,
				mainId:"",
				isView:false,//是否查看
				isDeal:false
			}
		},
		onLoad(options) {
			this.mainId = options.mainId;
			let type = options.type;
			
			if(type){
				this.isView = true;
				this.getDetail();
				//置灰
				this.config.forEach(item => {
					if(item.extra){
						item.extra.disabled = true;
					}
				})
				//增加处理说明
				this.config.push({
					name: 'disposeDescription',
					label:'处理说明',
					component: 'uni-easyinput',
					extra: {
						type: 'textarea',
						placeholder: "处理说明",
						disabled:true
					}
				});
				uni.setNavigationBarTitle({title:"交办详情"})
				if(type == "deal"){
					this.isDeal = true;
					uni.setNavigationBarTitle({title:"交办处理"})
					this.config[this.config.length-1].extra.disabled = false;
					this.rules.disposeDescription.rules[0].required = true;
				}
			}
			
		},
		methods: {
			posClick(){
				let _this = this;
				//跳转位置选择页面
				uni.navigateTo({
					url:"../../../common/webview/index?aurl="+encodeURI("pages/position"),
					events:{
						//监听选点事件，得到坐标 详细地址
						acceptDataFromOpenedPage:function(data){
							_this.form.address = data.coordinate.address;
							_this.form.lnglat = data.coordinate.lon+","+data.coordinate.lat;
						}
					}
				})
			},
			userClick(){
				this.$refs.popup.open()
			},
			radioChange(user){
				this.form.disposeUserId = user.id;
				this.form.disposeUserName = user.nickName;
				this.$refs.popup.close();
			},
			handleSubmit(){
				let _this = this;
				let form = this.$refs.uform.getForm()
				
				form.validate().then(res => {
					this.form.assignedUserId = this.$store.state.user.userInfo.id;
					this.form.assignedUserName = this.$store.state.user.userInfo.nickName;
					this.form.id = getUUID(32);
					this.form.assignedTime = new Date().getTime()
					// console.log(JSON.stringify(this.form));
					let isUpload = _this.$refs.attach.isUpload();
					if(isUpload){
						_this.$refs.attach.fileUpload("td_caw_case_assigned",this.form.id).then(res => {
							_this.submitData();
						});
					}else{
						_this.submitData();
					}
					
					
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
			
			submitData(){
				addCawCaseAssigned(this.form).then(res => {
					if(res.code == 200){
						this.$modal.msgSuccess("提交成功");
						this.$tab.navigateBack();
					}else{
						this.$modal.msgError(res.msg);
					}
				}).catch(e => {
					this.$modal.msgError("提交失败,请联系管理员处理");
				})
			},
			handleCancel(){
				this.$tab.navigateBack();
			},
			getDetail(){
				getCaseAssignedDetail(this.mainId).then(res => {
					if(res.code == 200){
						this.form = res.data;
					}
					return getAttachList({mianId:res.data.id,bussType:"td_caw_case_assigned"});
				}).then(res1 => {
					this.files = res1.rows.map(item => {
						return {
							name:item.fileName,
							extname:item.fileType,
							url:config.fileServerUrl+item.fileUrl
						}
					})
				})
			},
			handleDeal(){
				let _this = this;
				let form = this.$refs.uform.getForm()
				form.validate().then(res => {
					let params = {
						id:this.form.id,
						disposeTime:new Date().getTime(),
						disposeDescription:this.form.disposeDescription
					}
					console.log(JSON.stringify(params));
					updateCawCaseAssigned(params).then(res1 => {
						if(res1.code == 200){
							this.$modal.msgSuccess("提交成功");
							this.$tab.navigateBack();
						}else{
							this.$modal.msgError(res.msg);
						}
					})
				});
			}
		}
	}
</script>

<style>
	.popup-content {
		padding: 15px;
		height: 500px;
		background-color: #fff;
	}
	
</style>
